804b12
@@ -23,8 +23,6 @@
 import java.util.concurrent.SynchronousQueue;
 import java.util.concurrent.TimeUnit;
 
-import javax.servlet.ServletContextEvent;
-import javax.servlet.ServletContextListener;
 import javax.ws.rs.HttpMethod;
 
 import org.apache.hadoop.hive.common.metrics.common.Metrics;
@@ -43,6 +41,9 @@
 import org.apache.thrift.protocol.TBinaryProtocol;
 import org.apache.thrift.protocol.TProtocolFactory;
 import org.apache.thrift.server.TServlet;
+import org.eclipse.jetty.io.Connection;
+import org.eclipse.jetty.io.EndPoint;
+import org.eclipse.jetty.server.Connector;
 import org.eclipse.jetty.server.HttpConfiguration;
 import org.eclipse.jetty.server.HttpConnectionFactory;
 import org.eclipse.jetty.server.Server;
@@ -94,7 +95,21 @@
protected void initServer() {
           hiveConf.getIntVar(ConfVars.HIVE_SERVER2_THRIFT_HTTP_RESPONSE_HEADER_SIZE);
       conf.setRequestHeaderSize(requestHeaderSize);
       conf.setResponseHeaderSize(responseHeaderSize);
-      final HttpConnectionFactory http = new HttpConnectionFactory(conf);
+      final HttpConnectionFactory http = new HttpConnectionFactory(conf) {
+        public Connection newConnection(Connector connector, EndPoint endPoint) {
+          Connection connection = super.newConnection(connector, endPoint);
+          connection.addListener(new Connection.Listener() {
+            public void onOpened(Connection connection) {
+              openConnection();
+            }
+
+            public void onClosed(Connection connection) {
+              closeConnection();
+            }
+          });
+          return connection;
+        }
+      };
 
       boolean useSsl = hiveConf.getBoolVar(ConfVars.HIVE_SERVER2_USE_SSL);
       String schemeName = useSsl ? "https" : "http";
@@ -156,33 +171,6 @@
protected void initServer() {
         LOG.warn("XSRF filter disabled");
       }
 
-      context.addEventListener(new ServletContextListener() {
-        @Override
-        public void contextInitialized(ServletContextEvent servletContextEvent) {
-          Metrics metrics = MetricsFactory.getInstance();
-          if (metrics != null) {
-            try {
-              metrics.incrementCounter(MetricsConstant.OPEN_CONNECTIONS);
-              metrics.incrementCounter(MetricsConstant.CUMULATIVE_CONNECTION_COUNT);
-            } catch (Exception e) {
-              LOG.warn("Error reporting HS2 open connection operation to Metrics system", e);
-            }
-          }
-        }
-
-        @Override
-        public void contextDestroyed(ServletContextEvent servletContextEvent) {
-          Metrics metrics = MetricsFactory.getInstance();
-          if (metrics != null) {
-            try {
-              metrics.decrementCounter(MetricsConstant.OPEN_CONNECTIONS);
-            } catch (Exception e) {
-              LOG.warn("Error reporting HS2 close connection operation to Metrics system", e);
-            }
-          }
-        }
-      });
-
       final String httpPath = getHttpPath(hiveConf.getVar(HiveConf.ConfVars.HIVE_SERVER2_THRIFT_HTTP_PATH));
 
       if (HiveConf.getBoolVar(hiveConf, ConfVars.HIVE_SERVER2_THRIFT_HTTP_COMPRESSION_ENABLED)) {
@@ -209,6 +197,29 @@
public void contextDestroyed(ServletContextEvent servletContextEvent) {
     }
   }
 
+  private void openConnection() {
+    Metrics metrics = MetricsFactory.getInstance();
+    if (metrics != null) {
+      try {
+        metrics.incrementCounter(MetricsConstant.OPEN_CONNECTIONS);
+        metrics.incrementCounter(MetricsConstant.CUMULATIVE_CONNECTION_COUNT);
+      } catch (Exception e) {
+        LOG.warn("Error reporting HS2 open connection operation to Metrics system", e);
+      }
+    }
+  }
+
+  private void closeConnection() {
+    Metrics metrics = MetricsFactory.getInstance();
+    if (metrics != null) {
+      try {
+        metrics.decrementCounter(MetricsConstant.OPEN_CONNECTIONS);
+      } catch (Exception e) {
+        LOG.warn("Error reporting HS2 close connection operation to Metrics system", e);
+      }
+    }
+  }
+
   @Override
   public void run() {
     try {
